package com.example.administrator.szb.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.fragments.fragment_forTW.FragmentZDYH;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by D N on 2017/9/25.
 * 指定提问群体
 */

public class ZDYHActivity extends BaseActivity {
    @Bind(R.id.zdyh_image_fh)
    ImageView zdyhImageFh;
    @Bind(R.id.zdyh_image_search)
    ImageView zdyhImageSearch;
    @Bind(R.id.zdyh_edit_srnr)
    EditText zdyhEditSrnr;
    @Bind(R.id.toolbar7)
    Toolbar toolbar7;
    @Bind(R.id.textView47)
    TextView textView47;
    @Bind(R.id.zdyh_radiobutton_jrgw)
    RadioButton zdyhRadiobuttonJrgw;
    @Bind(R.id.zdyh_radiobutton_hyyh)
    RadioButton zdyhRadiobuttonHyyh;
    @Bind(R.id.radioGroup)
    RadioGroup radioGroup;
    @Bind(R.id.textView48)
    TextView textView48;
    @Bind(R.id.zdyh_framelayout_content)
    FrameLayout zdyhFramelayoutContent;
    @Bind(R.id.zdyh_text_qrtw)
    TextView zdyhTextQrtw;

    FragmentZDYH fragmentJRGW,fragmentHYYH;
    private FragmentManager fragmentManager;
    private ArrayList<Fragment> fragmentArrayList;
    private FragmentTransaction fragmentTransaction;

    Bundle bundle;
    private String param = "0";//搜索内容
    private int type = 0;//1用户2顾问0全部


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zdyh_activity);
        SampleApplicationLike.getActivitiesInstance().add(this);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        initFragment();
        zdyhEditSrnr.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
            /** * @param v * @param actionId 针对软键盘,若是实体键盘actionId=0 * @param event 针对实体键盘,若是软键盘event=null * @return 返回true表示自己处理Enter事件,当imeOptions="actionSearch"时返回false此方法会被调用两次 */ 
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
                if (actionId == EditorInfo.IME_ACTION_SEARCH || event.getKeyCode() == KeyEvent.ACTION_DOWN) {

                    if (type == 0) {
                        fragmentJRGW = null;
                        if (TextUtils.isEmpty(zdyhEditSrnr.getText().toString())){
                            param = "0";
                        }else{
                            param = zdyhEditSrnr.getText().toString();
                        }

                        chooseTab(type);
                    }else if(type == 1){
                        fragmentHYYH = null;
                        if (TextUtils.isEmpty(zdyhEditSrnr.getText().toString())){
                            param = "0";
                        }else{
                            param = zdyhEditSrnr.getText().toString();
                        }
                        chooseTab(type);
                    }



                    //Toasts.show(ZDYHActivity.this, "执行搜索", Toast.LENGTH_SHORT);
                     return true;
                } 
                            return false; 
            }
                        });

        
        
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i){
                    case R.id.zdyh_radiobutton_jrgw:
                        type = 0;
                        chooseTab(0);
                        break;
                    case R.id.zdyh_radiobutton_hyyh:
                        type = 1;
                        chooseTab(1);
                        break;
                }
            }
        });



    }
    private void initFragment() {
        fragmentManager = getSupportFragmentManager();
        fragmentArrayList = new ArrayList<Fragment>();
        chooseTab(0);
    }

    private void chooseTab(int i) {
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragments(fragmentTransaction);
        switch (i) {
            case 0:
                if(fragmentJRGW == null){
                    fragmentJRGW = new FragmentZDYH();
                    bundle = new Bundle();
                    bundle.putString("param",param);
                    bundle.putInt("type",2);
                    fragmentJRGW.setArguments(bundle);
                    changeFragment(fragmentJRGW);
                }else {
                    fragmentTransaction.show(fragmentJRGW);
                }
                break;
            case 1:
                if(fragmentHYYH == null){

                    fragmentHYYH =new FragmentZDYH();
                    bundle = new Bundle();
                    bundle.putString("param",param);
                    bundle.putInt("type_yhhy",1);
                    fragmentHYYH.setArguments(bundle);
                    changeFragment(fragmentHYYH);
                }else{
                    fragmentTransaction.show(fragmentHYYH);
                }
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }

    private void changeFragment(Fragment fragment) {
        fragmentArrayList.add(fragment);
        fragmentTransaction.add(R.id.zdyh_framelayout_content, fragment);
        fragmentTransaction.show(fragment);
    }

    private void hideFragments(FragmentTransaction fragmentTransaction) {
        for (Fragment fragment : fragmentArrayList) {
            fragmentTransaction.hide(fragment);
        }
    }

    @OnClick({R.id.zdyh_image_fh, R.id.zdyh_image_search, R.id.zdyh_text_qrtw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.zdyh_image_fh:
                finish();
                break;
            case R.id.zdyh_image_search:
                chooseTab(type);
                break;
            case R.id.zdyh_text_qrtw:
               finish();
                break;
        }
    }

}
